/*
 * @Author: wangyu
 * @Date: 2024-11-19 14:08:55
 * @LastEditTime: 2024-12-13 10:32:40
 * @LastEditors: wangyu
 * @Description:
 * @FilePath: \PreciseCultivationFront\src\utils\index.js
 */
/**
 *  通用js方法
 */

// 防抖
export function debounce(fn, time) {
  let timer = null;
  return function () {
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      fn.apply(this, arguments);
    }, time);
  };
}

// 节流
export function throttle(fn, time) {
  let activeTime = 0;
  return function () {
    const current = Date.now();
    if (current - activeTime >= time) {
      fn.apply(this, arguments);
      activeTime = Date.now();
    }
  };
}
// 深拷贝
export function deepClone(obj) {
  if (obj === null || typeof obj !== "object") return obj;
  if (Array.isArray(obj)) return obj.map(deepClone);

  const clone = {};
  for (const key in obj) {
    clone[key] = deepClone(obj[key]);
  }
  return clone;
}

/**
 * @description: 转千位分隔符数字
 * @param {*} num
 * @return {*}
 */
export function formatNumber(num) {
  return new Intl.NumberFormat("en-US").format(num);
}
